#include "CSaveState.h"

#include "CGame.h"
#include "CGamePlayState.h"

CSaveState::CSaveState()
{
	menuIndex = 0;
	menuCursor = -1;

	for(int i = 0; i < 3; ++i)
	{
		Profile prof;
		prof.index = -1;
		profileList.push_back(prof);
	}
}

CSaveState::~CSaveState()
{

}

CSaveState* CSaveState::GetInstance()
{
	static CSaveState instance;
	return &instance;
}

void CSaveState::Enter()
{
	menuCursor = CSGD_TextureManager::GetInstance()->LoadTexture("Resources/graphics/SGD_MenuCursor.png", D3DCOLOR_XRGB(255, 255, 255));
	LoadProfiles();
}

void CSaveState::LoadProfiles()
{
	ifstream reader;
	reader.open("Resources\\profiles.bin", ios::in | ios::binary);
	
	if(reader.is_open())
	{
		for(int i = 0; i < 3; ++i)
		{
			Profile tempProfile;

			reader.read((char*)&tempProfile.index, sizeof(int));		

			if(reader.eof())
			{
				reader.close();
				return;
			}

			if(tempProfile.index != -1)
			{
				reader.read(tempProfile.timePlayed, 25);
			}

			profileList[i] = tempProfile;
		}
		
		reader.close();
	}
}

void CSaveState::SaveProfiles()
{
	ofstream writer;
	writer.open("Resources/profiles.bin", ios::in | ios::binary);

	for(int i = 0; i < 3; ++i)
	{
		writer.write((char*)&profileList[i].index, sizeof(int));
		if(profileList[i].index != -1)
			writer.write(profileList[i].timePlayed, 25);
	}
	writer.close();
}

void CSaveState::NewProfile(int index)
{
	Profile newProfile;
	newProfile.index = index;
	strcpy_s(newProfile.timePlayed, 25, __TIMESTAMP__);
	//newProfile.timePlayed = __TIMESTAMP__;
	profileList[index] = newProfile;
}

bool CSaveState::Input()
{
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();
	
	if(pDI->KeyPressed(DIK_UPARROW))
	{
		if(--menuIndex < 0)
			menuIndex = 3;
	}
	else if(pDI->KeyPressed(DIK_DOWNARROW))
		if(++menuIndex > 3)
			menuIndex = 0;

	if(pDI->KeyPressed(DIK_RETURN))
	{
		switch(menuIndex)
		{
		case 0:
			if(profileList[0].index == -1)
			{
				NewProfile(0);
			}
			else
			{
				profileList[0].index = 0;
				strcpy_s(profileList[0].timePlayed, 25, __TIMESTAMP__);
			}
			break;
		case 1:
			if(profileList[1].index == -1)
			{
				NewProfile(1);
			}
			else
			{
				profileList[1].index = 1;
				strcpy_s(profileList[1].timePlayed, 25, __TIMESTAMP__);
			}
			break;
		case 2:
			if(profileList[2].index == -1)
			{
				NewProfile(2);
			}
			else
			{
				profileList[2].index = 2;
				strcpy_s(profileList[2].timePlayed, 25, __TIMESTAMP__);
			}
			break;
		case 3:
			CGamePlayState::GetInstance()->GetSavePause()->isPaused = false;
			CGame::GetInstance()->ChangeState(CGamePlayState::GetInstance());
			break;
		}
		SaveProfiles();
	}
	return false;
}

void CSaveState::Update(float fElapsedTime)
{
}

void CSaveState::Render()
{
	CSGD_Direct3D* pD3D = CSGD_Direct3D::GetInstance();

	pD3D->Clear(0, 0, 128);
	pD3D->DeviceBegin();

	pD3D->SpriteBegin();

	CSGD_TextureManager::GetInstance()->Draw(menuCursor, 80,200 + (menuIndex * 50)); //, 4.0f, 4.0f, NULL, 64.0f, 65.0f, 0, D3DCOLOR_ARGB(128, 255, 0, 0));

	pD3D->SpriteEnd();

	pD3D->DeviceEnd();
	pD3D->DrawText("Save Profile", 30, 50);
	pD3D->DrawText("Select profile to save...", 55, 100);
	pD3D->DrawText("Slot 1", 30, 200);
	pD3D->DrawText("Slot 2", 30, 250);
	pD3D->DrawText("Slot 3", 30, 300);
	
	for(int i = 0; i < 3; ++i)
	{
		if(profileList[i].index == -1)
			pD3D->DrawText("Empty", 100, 200 + (i * 50));
		else
		{
			char buff[50];
			sprintf_s(buff, "Profile %i", profileList[i].index + 1);
			pD3D->DrawText(buff, 100, 200 + (i * 50));
			
			sprintf_s(buff, 25, "Last Play Time: ");
			strcat_s(buff, 50, profileList[i].timePlayed);

			pD3D->DrawText(buff, 150, 200 + (i * 50) + 25);
		}
	}
	pD3D->DrawText("Quit", 100, 350);

	pD3D->Present();
}

void CSaveState::Exit()
{
}